import java.io.*;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86186
 * Date: 2023-10-07
 * Time: 14:46
 */
public class Demo1 {
    public static void main(String[] args) {
        //复制文件到指定路径
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要复制的文件：");
        String srcPath = scanner.next();
        File srcFile = new File(srcPath);
        if (!srcFile.exists()) {
            System.out.println("您输入的文件不存在，请确认文件路径的正确性");
            return;
        }
        if (!srcFile.isFile()) {
            System.out.println("您输入的文件不是普通文件，请确认文件路径的正确性");
            return;
        }

        System.out.println("请输入要复制到的目标路径");
        String desPath = scanner.next();
        File desFile = new File(desPath);
        if (desFile.isDirectory()) {
            System.out.println("您输入的路径是一个目录，不是文件，请确认文件路径是否正确");
            return;
        }

        if (desFile.isFile()) {
            if (desFile.exists()) {
                System.out.println("您要复制到的文件已存在，是否需要覆盖：Y/N");
                String choice = scanner.next();
                if (!choice.equals("Y")) return;
            }
        }

        try (InputStream inputStream = new FileInputStream(srcFile)) {
            try (OutputStream outputStream = new FileOutputStream(desFile)) {
                byte[] b = new byte[1024];
                while (true) {
                    int n = inputStream.read(b);
                    if (n == -1) break;
                    outputStream.write(b,0,n);
                }
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
